package com.wl.api.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.wl.api.common.utils.CollectionUtil;
import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.ObjectUtil;
import com.wl.api.common.utils.constant.ClientConstant;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.system.SecurityAreaLadderExtendDTO;
import com.wl.api.model.system.SecurityChannelAreaLadderPrice;
import com.wl.api.service.system.ISecurityChannelAreaLadderPriceService;

@RestController
@RequestMapping("/admin/area/ladder")
public class AdminAreaLadderController extends CommonController {

	@Autowired
	ISecurityChannelAreaLadderPriceService areaLadderPriceService;

	@RequestMapping(value="/manage", method=RequestMethod.POST)
	public JsonObject create(@RequestBody SecurityAreaLadderExtendDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "", code = "";
		if (validate().test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if(pass) {
			CommonUserDTO user = getCommonUser();
			List<SecurityChannelAreaLadderPrice> deliveryLadderList = param.getDeliveryLadderList().parallelStream()
					.map(s -> SecurityChannelAreaLadderPrice.builder().feeType(ClientConstant.fee_type_delivery).areaId(param.getCountryId()).amount(s.getAmount()).weightFrom(s.getWeightFrom()).weightTo(s.getWeightTo()).updateUser(user.getUsername()).updateUserId(user.getId()).createTime(new Date()).build()).collect(Collectors.toList());
			
			List<SecurityChannelAreaLadderPrice> pickUpLadderList = param.getPickUpLadderlList().parallelStream()
					.map(s -> SecurityChannelAreaLadderPrice.builder().feeType(ClientConstant.FEE_TYPE_PICK_UP).areaId(param.getCountryId()).amount(s.getAmount()).weightFrom(s.getWeightFrom()).weightTo(s.getWeightTo()).updateUser(user.getUsername()).updateUserId(user.getId()).createTime(new Date()).build()).collect(Collectors.toList());
			areaLadderPriceService.insertLadderPriceBatch(deliveryLadderList, pickUpLadderList);
			json.setData(param.getCountryId());
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	/**
	 *  修改产品数据
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/manage", method = RequestMethod.PUT)
	public JsonObject modify(@RequestBody SecurityAreaLadderExtendDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "" , error_code = ""; 
		if (validate().test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			error_code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		
		if(pass) {
			CommonUserDTO user = getCommonUser();
			List<SecurityChannelAreaLadderPrice> list = new ArrayList<>();
			List<SecurityChannelAreaLadderPrice> deliveryLadderList = param.getDeliveryLadderList().parallelStream()
					.map(s -> SecurityChannelAreaLadderPrice.builder().id(s.getId()).feeType(ClientConstant.fee_type_delivery).areaId(param.getCountryId()).amount(s.getAmount()).weightFrom(s.getWeightFrom()).weightTo(s.getWeightTo()).updateUser(user.getUsername()).updateUserId(user.getId()).createTime(new Date()).build()).collect(Collectors.toList());
			list.addAll(deliveryLadderList);
			List<SecurityChannelAreaLadderPrice> pickUpLadderList = param.getPickUpLadderlList().parallelStream()
					.map(s -> SecurityChannelAreaLadderPrice.builder().id(s.getId()).feeType(ClientConstant.FEE_TYPE_PICK_UP
							).areaId(param.getCountryId()).amount(s.getAmount()).weightFrom(s.getWeightFrom()).weightTo(s.getWeightTo()).updateUser(user.getUsername()).updateUserId(user.getId()).createTime(new Date()).build()).collect(Collectors.toList());
			list.addAll(pickUpLadderList);
			areaLadderPriceService.updateLadderPriceBatch(list, param.getCountryId());
		}
		setJsonMessage(json, title, error_code, pass);
		return json;
	}
	
	@RequestMapping(value="/manage", method = RequestMethod.GET)
	public JsonObject getItemList(SecurityAreaLadderExtendDTO param) {
		JsonObject json = JsonObject.getInstance();
		json.setData(areaLadderPriceService.getItemList(param));
		setJsonMessage(json);
		return json;
		
	}
	
	@RequestMapping(value="/manage/{countryId}", method = RequestMethod.GET)
	public JsonObject getDetail(@PathVariable("countryId") Integer countryId) {
		JsonObject json = JsonObject.getInstance();
		json.setData(areaLadderPriceService.getAreaLadderDetail(SecurityAreaLadderExtendDTO.builder().countryId(countryId).build()));
		setJsonMessage(json);
		return json;
		
	}

	private Predicate<SecurityAreaLadderExtendDTO> validate() {
		return  s -> ObjectUtil.isEmpty(s.getCountryId()) || CollectionUtil.isEmpty(s.getDeliveryLadderList())
				   || CollectionUtil.isEmpty(s.getPickUpLadderlList()) || s.getDeliveryLadderList().parallelStream().anyMatch(a -> ObjectUtil.isEmpty(a.getAmount())  || ObjectUtil.isEmpty(a.getWeightFrom()) || ObjectUtil.isEmpty(a.getWeightTo()))
				    ||  s.getPickUpLadderlList().parallelStream().anyMatch(a -> ObjectUtil.isEmpty(a.getAmount())  || ObjectUtil.isEmpty(a.getWeightFrom()) || ObjectUtil.isEmpty(a.getWeightTo()))
				   ;
	}
}
